package com.hqully.labmanage.utils;

import java.time.Duration;
import java.time.LocalDateTime;

/**
 * <p>
 *     时间格式处理工具
 * </p>
 * @author liulingyu
 * @date 2022-05-07 16:02
 * @version 1.0
 */
public class TimeUtil {

    /**
     *<p>
     *     求两个LocalDateTime之间的差值，并格式化
     *</p>
     * @date 2022/5/7 16:03 <br>
     * @author liulingyu <br>
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @return java.lang.String 时长
     */
    public static String formatDuration(LocalDateTime startTime,LocalDateTime endTime) {
        Duration duration = Duration.between(startTime,endTime);
        long seconds = duration.getSeconds();
        long absSeconds = Math.abs(seconds);
        String positive = String.format(
                "%d小时%02d分钟%02d秒",
                absSeconds / 3600,
                (absSeconds % 3600) / 60,
                absSeconds % 60);
        return seconds < 0 ? "-" + positive : positive;}
}
